package com.xjiuge.fruit.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.xjiuge.fruit.service.base.BaseServiceImpl;
import com.xjiuge.fruit.bean.Menu;
import com.xjiuge.fruit.bean.Role;
import com.xjiuge.fruit.bean.RoleMenu;
import com.xjiuge.fruit.bean.User;
import com.xjiuge.fruit.common.ComIdUtil;
import com.xjiuge.fruit.mapper.MenuMapper;
import com.xjiuge.fruit.mapper.RoleMapper;
import com.xjiuge.fruit.mapper.RoleMenuMapper;
import com.xjiuge.fruit.service.RoleService;

import org.springframework.stereotype.Service;

@Service("roleService")
public class RoleServiceImpl 
    extends BaseServiceImpl<Role,RoleMapper> 
    implements RoleService {

	@Resource(name = "roleMapper")
	private RoleMapper roleMapper;
	@Resource(name = "menuMapper")
	private MenuMapper menuMapper;
	@Resource(name = "roleMenuMapper")
	private RoleMenuMapper roleMenuMapper;
	/**
	 * 添加角色
	 * RoleServiceImpl.save<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月7日  下午3:44:11</p>
	 * @param role
	 * @param user
	 * @param ids
	 */
	@Override
	public void save(Role role, User user, String[] ids) {
		String uuid = ComIdUtil.getUUID();
		
		role.setRoleId(uuid);
		role.setCreaterId(user.getUserId());
		role.setCreaterName(user.getUserName());
		role.setCreateTime(new Date());
		roleMapper.insertSelective(role);
		
		for (String menuId : ids) {
			RoleMenu roleMenu = new RoleMenu();
			roleMenu.setRoleMenuId(ComIdUtil.getUUID());
			roleMenu.setRoleId(uuid);
			roleMenu.setRoleName(role.getRoleName());
			roleMenu.setMenuId(menuId);
			Menu menu = menuMapper.selectByPrimaryKey(menuId);
			roleMenu.setMenuName(menu.getMenuName());
			roleMenu.setCreaterId(user.getUserId());
			roleMenu.setCreaterName(user.getUserName());
			roleMenu.setCreateTime(new Date());
			roleMenuMapper.insertSelective(roleMenu);
		}
	}
	
	/**
	 * 查询所有角色列表
	 * RoleServiceImpl.selectAllRole<BR>
	 * <p>Author:xuhongcheng</p>
	 * <p>Date: 2017年11月7日  下午4:07:05</p>
	 * @return
	 */
	@Override
	public List<RoleMenu> selectAllRole() {
		List<RoleMenu> resultList = new ArrayList<>();
		List<RoleMenu> roleMenuList = roleMenuMapper.selectAll();
		Map<String, Object> map = new HashMap<String, Object>();
		for (RoleMenu roleMenu : roleMenuList) {
			map.put(roleMenu.getRoleMenuId()+"name", roleMenu.getMenuName());
			map.put(roleMenu.getRoleMenuId()+"id", roleMenu.getMenuId());
			if(!map.containsKey(roleMenu.getRoleId())){
				roleMenu.setAuthName(roleMenu.getMenuName());
				roleMenu.setAuthId(roleMenu.getMenuId());
				resultList.add(roleMenu);
				map.put(roleMenu.getRoleId(), roleMenu.getRoleId());
			}else{
				for (RoleMenu rm : resultList) {
					if(rm.getRoleId().equals(roleMenu.getRoleId())){
						rm.setAuthName(rm.getAuthName() + ","+map.get(roleMenu.getRoleMenuId()+"name"));
						rm.setAuthId(rm.getMenuId() + ","+map.get(roleMenu.getRoleMenuId()+"id"));
					}
				}
			}
		}
		return resultList;
	}


}